﻿using WcfGeebaServer.DBDAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using WcfGeebaServer.ContentManage;
using WcfGeebaServer.Users;

namespace TestGeeba
{
    
    
    /// <summary>
    ///This is a test class for ThreadDATest and is intended
    ///to contain all ThreadDATest Unit Tests
    ///</summary>
    [TestClass()]
    public class ThreadDATest
    {
        private GSubForum sf;
        private ThreadDA target;
        public ThreadDATest()
        {
            /*Data.subForums.Clear();
            Data.subForumsThreads.Clear();
            Data.moderators.Clear();
            Data.threadsPosts.Clear(); // clear posts*/
            Data.ClearDB();

            target = ThreadDA.GetThreadDA();

            sf = new GSubForum("topic");
            new SubForumDA().AddSubForum(sf);
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion



        /// <summary>
        ///A test for GetThreadDA
        ///</summary>
        [TestMethod()]
        public void GetThreadDATest()
        {
            Assert.IsNotNull(ThreadDA.GetThreadDA());
        }

        /// <summary>
        ///A test for AddThread
        ///</summary>
        [TestMethod()]
        public void AddThreadTest()
        {
            Member publisher = new Member("username1", "1234", true);
            GThread thread = new GThread("a1", "b1", publisher);
            int actual = target.AddThread(thread, sf);
            Assert.IsTrue(actual > 0);

        }

        /// <summary>
        ///A test for AddPost
        ///</summary>
        [TestMethod()]
        public void AddPostTest()
        {
            GThread parent = new GThread("parent2", "parentContent", new Member("admin", "1234", true));
            parent = new GThread(target.AddThread(parent, sf), parent);

            GPost post = new GPost("a2", "b2", new Member("username3", "1234", true), parent);
            int actual  = target.AddPost(post, parent);
            Assert.IsTrue(actual > 0);
        }


        /// <summary>
        ///A test for RemovePost
        ///</summary>
        [TestMethod()]
        public void RemovePostTest()
        {
            Member publisher = new Member("username4", "1234", true);
            GThread parent = new GThread("parent5", "parentContent", new Member("admin6", "1234", true));
            GPost post = new GPost("a2", "b2", publisher, parent);

            Assert.IsFalse(target.RemovePost(post, parent));

            new SubForumDA().AddSubForum(sf);
            parent = new GThread(target.AddThread(parent, sf), parent);
            int postId = target.AddPost(post, parent);
            bool actual = target.RemovePost(new GPost(postId, "a7", "b7", publisher, parent), parent);
            Assert.IsTrue(actual);
        }

        /// <summary>
        ///A test for RemoveThread
        ///</summary>
        [TestMethod()]
        public void RemoveThreadTest()
        {
            GThread thread = new GThread("a8", "b8", new Member("usernam9", "1234", true));
            bool actual = target.RemoveThread(thread, sf);
            Assert.IsFalse(actual);

            new SubForumDA().AddSubForum(sf);
            int threadId = target.AddThread(thread, sf);
            actual = target.RemoveThread(new GThread(threadId, "a10", "b10", new Member("usernam11", "1234", true)) , sf);
            Assert.IsTrue(actual);

        }

        [TestMethod()]
        public void EditThreadContentTest()
        {
            string content = "some content";
            string newContent = "some new content";
            Member publisher = new Member("username00", "1234", true);
            GThread thread = new GThread("a123", content, publisher);
            int threadID = target.AddThread(thread, sf);
            thread = target.GetThread(threadID, sf);
            thread.Content = newContent;
            target.EditThreadContent(thread);
            Assert.AreEqual(newContent, target.GetThread(threadID, sf).Content);
        }

        [TestMethod()]
        public void GetThreadTest()
        {
            string subject = "subject123";
            string content = "content123";
            Member publisher = new Member("username000", "1234", true);
            GThread thread = new GThread(subject, content, publisher);
            int threadID = target.AddThread(thread, sf);
            Assert.AreEqual(content, target.GetThread(threadID, sf).Content);
            Assert.AreEqual(subject, target.GetThread(threadID, sf).Subject);
            Assert.AreEqual(threadID, target.GetThread(threadID, sf).Id);
        }

        [TestMethod()]
        public void GetPostsTest()
        {
            Member publisher = new Member("publisher123", "1234", true);
            GThread thread = new GThread("thread", "content", publisher);
            int threadID = target.AddThread(thread, sf);
            thread = target.GetThread(threadID, sf);
            int count = 0;
            if (target.GetPosts(thread) != null)
                count = target.GetPosts(thread).Count;
            target.AddPost(new GPost("bla", "bla", publisher, thread), thread);
            target.AddPost(new GPost("bla1", "bla", publisher, thread), thread);
            target.AddPost(new GPost("bla2", "bla", publisher, thread), thread);
            Assert.IsTrue(count + 3 == target.GetPosts(thread).Count);
        }

        [TestMethod()]
        public void GetPostTest()
        {
            Member publisher = new Member("publisher123", "1234", true);
            GThread thread = new GThread("thread12", "content", publisher);
            int threadID = target.AddThread(thread, sf);
            thread = target.GetThread(threadID, sf);
            string subject = "subjectABC";
            string content = "contentABC";
            int postID = target.AddPost(new GPost(subject, content, publisher, thread), thread);
            Assert.IsTrue(content.Equals(target.GetPost(postID, thread).Content));
            Assert.IsTrue(subject.Equals(target.GetPost(postID, thread).Subject));
            subject = "subjectABC123";
            content = "contentABC123";
            thread = target.GetPost(postID, thread);
            postID = target.AddPost(new GPost(subject, content, publisher, thread), thread);
            Assert.IsTrue(content.Equals(target.GetPost(postID, thread).Content));
            Assert.IsTrue(subject.Equals(target.GetPost(postID, thread).Subject));
        }

        [TestMethod()]
        public void findThreadsByContentTest()
        {
            Member publisher = new Member("username1", "1234", true);
            GThread thread = new GThread("a1", "b1", publisher);
            GThread thread2 = new GThread("a1", "b1 some words", publisher);
            GThread thread3 = new GThread("a1", "b2 some words b1", publisher);
            GThread thread4 = new GThread("a1", "b2 some words", publisher);
            target.AddThread(thread, sf);
            target.AddThread(thread2, sf);
            target.AddThread(thread3, sf);
            target.AddThread(thread4, sf);

            Assert.IsTrue(target.findThreadsByContent("b1").Count == 3);
        }

        [TestMethod()]
        public void GetSubforumTest()
        {
            Member publisher = new Member("username1", "1234", true);
            GThread thread = new GThread("a1", "b1", publisher);
            target.AddThread(thread, sf);
            thread = new GThread(target.AddThread(thread, sf),thread);
            Assert.IsTrue(target.GetSubforum(thread).Topic == sf.Topic);
        }

        [TestMethod()]
        public void findThreadsByUserTest()
        {
            Member publisher = new Member("username1", "1234", true);
            Member publisher2 = new Member("username2", "1234", true);
            GThread thread = new GThread("a1", "b1", publisher);
            GThread thread2 = new GThread("a1", "b1 some words", publisher);
            GThread thread3 = new GThread("a1", "b2 some words b1", publisher);
            GThread thread4 = new GThread("a1", "b2 some words", publisher);
            GThread thread5 = new GThread("a1", "b2 some words", publisher2);
            target.AddThread(thread, sf);
            target.AddThread(thread2, sf);
            target.AddThread(thread3, sf);
            target.AddThread(thread4, sf);
            target.AddThread(thread5, sf);

            Assert.IsTrue(target.findThreadsByUser(publisher.Username).Count == 4);
        }
        
        [TestMethod()]
        public void getTopicRootTest()
        {
            Member publisher = new Member("username1", "1234", true);
            GThread thread = new GThread("a1", "b1", publisher);
            thread = new GThread(target.AddThread(thread, sf), thread);
            GPost post1 = new GPost("a1", "b1 some words", publisher,thread);
            post1 = new GPost(target.AddPost(post1, thread), post1);
            GPost post2 = new GPost("a1", "b1 some words", publisher, post1);
            post2 = new GPost(target.AddPost(post2, post1), post2);
            GPost post3 = new GPost("a1", "b1 some words", publisher, post1);
            post3 = new GPost(target.AddPost(post3, post1), post3);
            GPost post4 = new GPost("a1", "b1 some words", publisher, post2);
            post4 = new GPost(target.AddPost(post4, post2), post4);
            GPost post5 = new GPost("a1", "b1 some words", publisher, post2);
            post5 = new GPost(target.AddPost(post5, post2), post5);
            GPost post6 = new GPost("a1", "b1 some words", publisher, post3);
            post6 = new GPost(target.AddPost(post6, post3),post6);

            Assert.IsTrue(target.getTopicRoot(post1).Equals(thread));
            Assert.IsTrue(target.getTopicRoot(post2).Equals(thread));
            Assert.IsTrue(target.getTopicRoot(post3).Equals(thread));
            Assert.IsTrue(target.getTopicRoot(post4).Equals(thread));
            Assert.IsTrue(target.getTopicRoot(post5).Equals(thread));
            Assert.IsTrue(target.getTopicRoot(post6).Equals(thread));
        }
    }
}
